{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<html><body><div>\n",
      "    <ul>\n",
      "         <li class=\"item-0\"><a href=\"link1.html\">first item</a></li>\n",
      "         <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
      "         <li class=\"item-inactive\"><a href=\"link3.html\"><span class=\"bold\">third item</span></a></li>\n",
      "         <li class=\"item-1\"><a href=\"link4.html\">fourth item</a></li>\n",
      "         <li class=\"item-0\"><a href=\"link5.html\">fifth item</a></li>\n",
      "     </ul>\n",
      " </div>\n",
      "</body></html>\n"
     ]
    }
   ],
   "source": [
    "from lxml import etree\n",
    "\n",
    "text = '''\n",
    "<div>\n",
    "    <ul>\n",
    "         <li class=\"item-0\"><a href=\"link1.html\">first item</a></li>\n",
    "         <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
    "         <li class=\"item-inactive\"><a href=\"link3.html\"><span class=\"bold\">third item</span></a></li>\n",
    "         <li class=\"item-1\"><a href=\"link4.html\">fourth item</a></li>\n",
    "         <li class=\"item-0\"><a href=\"link5.html\">fifth item</a></li>\n",
    "     </ul>\n",
    " </div>\n",
    "'''\n",
    "html = etree.HTML(text)\n",
    "result = etree.tostring(html)\n",
    "\n",
    "with open(\"./hello.html\",'wb') as wh:\n",
    "    wh.write(result)\n",
    "\n",
    "print result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<html>\n",
      "  <body><div>\n",
      "    <ul>\n",
      "         <li class=\"item-0\"><a href=\"link1.html\">first item</a></li>\n",
      "         <li class=\"item-1\"><a href=\"link2.html\">second item</a></li>\n",
      "         <li class=\"item-inactive\"><a href=\"link3.html\"><span class=\"bold\">third item</span></a></li>\n",
      "         <li class=\"item-1\"><a href=\"link4.html\">fourth item</a></li>\n",
      "         <li class=\"item-0\"><a href=\"link5.html\">fifth item</a></li>\n",
      "     </ul>\n",
      " </div>\n",
      "</body>\n",
      "</html>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "html = etree.parse('hello.html')\n",
    "result = etree.tostring(html,pretty_print=True)\n",
    "print result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<type 'lxml.etree._ElementTree'>\n"
     ]
    }
   ],
   "source": [
    "print type(html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element li at 0x3cbeac8>, <Element li at 0x3cbebc8>, <Element li at 0x3cbec08>, <Element li at 0x3cbec48>, <Element li at 0x3cbec88>]\n",
      "5\n",
      "<type 'list'>\n",
      "<type 'lxml.etree._Element'>\n"
     ]
    }
   ],
   "source": [
    "result = html.xpath('//li')\n",
    "print result\n",
    "print len(result)\n",
    "print type(result)\n",
    "print type(result[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['item-0', 'item-1', 'item-inactive', 'item-1', 'item-0']\n"
     ]
    }
   ],
   "source": [
    "result = html.xpath('//li/@class')\n",
    "print result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element a at 0x3cbea88>]\n"
     ]
    }
   ],
   "source": [
    "result = html.xpath('//li/a[@href=\"link1.html\"]')\n",
    "print result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element span at 0x3cbecc8>]\n"
     ]
    }
   ],
   "source": [
    "# / 是用来获取子元素的，而 <span> 并不是 <li> 的子元素，所以，要用双斜杠\n",
    "result = html.xpath('//li//span')\n",
    "print result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['bold']\n"
     ]
    }
   ],
   "source": [
    "# 获取 <li> 标签下的所有 class，不包括 <li>\n",
    "\n",
    "result = html.xpath('//li/a//@class')\n",
    "print result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['link5.html']\n"
     ]
    }
   ],
   "source": [
    "# 获取最后一个 <li> 的 <a> 的 href\n",
    "\n",
    "result = html.xpath('//li[last()]/a/@href')\n",
    "print result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element a at 0x55620c8>]\n",
      "fourth item\n"
     ]
    }
   ],
   "source": [
    "result = html.xpath('//li[last()-1]/a')\n",
    "print result\n",
    "print result[0].text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "span\n"
     ]
    }
   ],
   "source": [
    "result = html.xpath('//*[@class=\"bold\"]')\n",
    "print result[0].tag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
